package com.yejing.zxingdemo;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
 
public class AES128 {
 
    private static Logger logger = LoggerFactory.getLogger(AES128.class);
 
    static final String KEY_ALGORITHM = "AES";
    static final String CIPHER_ECB_PKCS5 = "AES/ECB/PKCS5Padding";
 
 
    /**
     * 加密
     *
     * @param src   密码
     * @param seed  应用的secretid
     * @return  加密后的密码
     */
    public static String encrypt(String src, String seed) {
        seed = formatSeed(seed);
        try {
            byte[] result = encode(src.getBytes("utf-8"), seed.getBytes());
            return bytesToHexString(result);
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return null;
    }
 
    /**
     * 解密
     *
     * @param decryptSrc 已加密的密码
     * @param seed 应用的secretid
     * @return 解密
     */
    public static String decrypt(String decryptSrc, String seed) {
        seed = formatSeed(seed);
        try {
            byte[] enc = hexStringToBytes(decryptSrc);
            byte[] result = decode(enc, seed.getBytes());
            if (result != null) {
                return new String(result);
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return null;
    }
 
    private static byte[] encode(byte[] clear, byte[] raw) {
        try {
            SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_ALGORITHM);
            Cipher cipher = Cipher.getInstance(CIPHER_ECB_PKCS5);
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            byte[] encrypted = cipher.doFinal(clear);
            return encrypted;
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return null;
    }
 
    private static byte[] decode(byte[] encrypted, byte[] raw) {
        try {
            SecretKeySpec skeySpec = new SecretKeySpec(raw, KEY_ALGORITHM);
            Cipher cipher = Cipher.getInstance(CIPHER_ECB_PKCS5);
            cipher.init(Cipher.DECRYPT_MODE, skeySpec);
            byte[] decrypted = cipher.doFinal(encrypted);
            return decrypted;
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return null;
    }
 
    private static byte[] hexStringToBytes(String hexString) {
        if (hexString == null || "".equals(hexString)) {
            return null;
        }
        hexString = hexString.toUpperCase();
        int length = hexString.length() / 2;
        char[] hexChars = hexString.toCharArray();
        byte[] d = new byte[length];
        for (int i = 0; i < length; i++) {
            int pos = i * 2;
            d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]) & 0xff);
        }
        return d;
    }
 
    private static String formatSeed(String seed) {
        if (org.apache.commons.lang3.StringUtils.isBlank(seed)) {
            seed = "";
        }
        if (seed.length() == 16) {
            return seed;
        }
        seed = org.apache.commons.lang3.StringUtils.rightPad(seed, 16, "0");
        return seed.substring(0, 16);
    }
 
    private static byte charToByte(char c) {
        return (byte) "0123456789ABCDEF".indexOf(c);
    }
 
    private static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }
 
   //示例
    public static void main(String[] args) {
        String password="password";
        String secretid="16位的字符串";  // #请修改为应用的secretid
        String result=encrypt(password,secretid);
        System.out.println(result);

        String str = "";
    }
}